package com.jsh.erp.service.order;

import com.jsh.erp.datasource.entities.User;
import com.jsh.erp.datasource.entities.UserBusiness;
import com.jsh.erp.service.ICommonQuery;
import com.jsh.erp.service.userBusiness.UserBusinessService;
import com.jsh.erp.utils.Constants;
import com.jsh.erp.utils.QueryUtils;
import com.jsh.erp.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * gaoqijun
 */
@Service(value = "order_component")
@OrderResource
public class OrderComponent implements ICommonQuery {

    @Resource
    private OrderService orderService;
    @Resource
    private UserBusinessService userBusinessService;

    @Override
    public Object selectOne(Long id) throws Exception {
        return orderService.getOrder(id);
    }

    @Override
    public List<?> select(Map<String, String> map,HttpServletRequest request)throws Exception {
        return getOrderList(map,request);
    }

    private List<?> getOrderList(Map<String, String> map,HttpServletRequest request) throws Exception{
        String search = map.get(Constants.SEARCH);

        String khxx = StringUtil.getInfo(search, "khxx");
        String guige = StringUtil.getInfo(search, "guige");
        String beignTime = StringUtil.getInfo(search, "beginTime");
        String endTime = StringUtil.getInfo(search, "endTime");
        String beizhu = StringUtil.getInfo(search, "beizhu");
        String ddNum = StringUtil.getInfo(search, "ddNum");
        String type = StringUtil.getInfo(search, "type");
        String status = StringUtil.getInfo(search, "status");
        String order = QueryUtils.order(map);
        String jingshouren = null;
        User curUser = (User) request.getSession().getAttribute("user");
        if (curUser.getPosition().contains("普通")){
            jingshouren = String.valueOf(curUser.getId());
        }
        return orderService.select(khxx, beizhu,guige,beignTime,ddNum,endTime,type,status,jingshouren,QueryUtils.offset(map), QueryUtils.rows(map));
    }

    @Override
    public Long counts(Map<String, String> map,HttpServletRequest request) throws Exception{
        String search = map.get(Constants.SEARCH);
        String khxx = StringUtil.getInfo(search, "khxx");
        String guige = StringUtil.getInfo(search, "guige");
        String beignTime = StringUtil.getInfo(search, "beginTime");
        String endTime = StringUtil.getInfo(search, "endTime");
        String beizhu = StringUtil.getInfo(search, "beizhu");
        String ddNum = StringUtil.getInfo(search, "ddNum");
        String type = StringUtil.getInfo(search, "type");
        String status = StringUtil.getInfo(search, "status");
        String jingshouren = null;
        User curUser = (User) request.getSession().getAttribute("user");
        if (curUser.getPosition().contains("普通")){
            jingshouren = String.valueOf(curUser.getId());
        }
        return orderService.countOrder(khxx, beizhu,guige,beignTime,endTime,ddNum,type,status,jingshouren);
    }

    @Override
    public int insert(String beanJson, HttpServletRequest request)throws Exception {
        return orderService.insertOrder(beanJson, request);
    }

    @Override
    public int update(String beanJson, Long id, HttpServletRequest request)throws Exception {
        return orderService.updateOrder(beanJson, id, request);
    }

    @Override
    public int delete(Long id, HttpServletRequest request)throws Exception {
        return orderService.deleteOrder(id, request);
    }

    @Override
    public int batchDelete(String ids, HttpServletRequest request)throws Exception {
        return orderService.batchDeleteOrder(ids, request);
    }

    @Override
    public int checkIsNameExist(Long id, String name)throws Exception {
        return orderService.checkIsNameExist(id, name);
    }

}
